package top.livepan.system.controller;

import com.livepan.server.BaseController;
import com.livepan.server.error.BusinessException;
import com.livepan.server.response.CommonReturnType;
import com.livepan.server.service.RoleResourceService;
import com.livepan.server.service.model.RoleResourceModel;
import com.livepan.server.service.model.PageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/roleResource")
public class RoleResourceController extends BaseController {

    public static final String BUSINESS_NAME = "角色资源关联";

    private static final Logger LOG = LoggerFactory.getLogger(RoleResourceController.class);

    @Autowired
    private RoleResourceService roleResourceService;

    @ResponseBody
    @RequestMapping("/list")
    public CommonReturnType roleResource(@RequestParam(value = "roleId") String roleId) {
        List<RoleResourceModel> roleResourceModelList = roleResourceService.list(roleId);
        return CommonReturnType.create(roleResourceModelList);
    }


    @ResponseBody
    @RequestMapping("/saveRoleResource")
    public CommonReturnType saveRoleResource(@RequestParam String roleId, @RequestBody List<String> resourceList) throws BusinessException {

        roleResourceService.clear(roleId);
        for (String resource : resourceList) {
            RoleResourceModel roleResourceModel = new RoleResourceModel();
            roleResourceModel.setResourceId(resource);
            roleResourceModel.setRoleId(roleId);
            roleResourceService.save(roleResourceModel);
        }
        return CommonReturnType.create(null);
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public CommonReturnType delete(@PathVariable String id) {
        roleResourceService.delete(id);
        return CommonReturnType.create(null);
    }
}
